package com.baymax.test.scene.demo

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.Typeface
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.setPadding
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newSceneFlexContainer
import com.baymax.test.scene.view.BlurDrawable
import com.google.android.flexbox.FlexboxLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SurfaceScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newSceneFlexContainer {
            val scene = this@SurfaceScene
            val parent = this
            parent.setBackgroundColor(Color.WHITE)
            addButton("toggleLayer ") {
                parent.toggleLayer()
                this.text = "toggle layer ${parent.layerType}"
            }
            val case = SurfaceCase(parent, scene)
            case.start()
            addButton("toggle inner") {
                case.toggleInnerLayer()
            }
            addButton("toggle inner hard") {
                case.toggleHard()
            }
        }
    }


    class SurfaceCase(val parent: FlexboxLayout, val scene: BaseScene) {

        val context = parent.context
        val surfaceView: SurfaceView = SurfaceView(context)
        val frame = FrameLayout(context)

        init {
            surfaceView.isFocusable = true
            frame.clipChildren = false
            frame.clipToPadding = false
            frame.setPadding(12)
            val target = surfaceView
            val overlay = surfaceView.overlay
//            surfaceView.setZOrderOnTop(true)
            surfaceView.setOnFocusChangeListener { v, hasFocus ->
                if (target is ViewGroup) {
                    target.setWillNotDraw(false)
                }
                val blurDrawable = BlurDrawable()
                overlay.add(blurDrawable)
                blurDrawable.setBounds(0, 0, target.width, target.height)
                blurDrawable.start()
            }

            parent.addView(frame, FlexboxLayout.LayoutParams(400, 200))
            frame.addView(surfaceView, FlexboxLayout.LayoutParams(300, 100))
        }

        private fun dumpInfo(v: View) {
            var current: View? = v
            while (current != null) {
                Log.w(TAG, String.format("dumpInfo current:%s ,%s", current.layerType, current))
                current = current.parent as? View
            }
        }

        fun start() {
            val color = com.baymax.test.scene.drawable.ColorUtils.createColorStateList(
                Color.RED, Color.YELLOW, Color.BLACK

            )
            val p = Paint(Paint.ANTI_ALIAS_FLAG).apply {
                setColor(color.getColorForState(surfaceView.drawableState, 0))
                setTypeface(Typeface.SANS_SERIF)
                setTextSize(60f)
                setStyle(Paint.Style.FILL)
            }
            scene.launch(Dispatchers.Default) {
                var index = 0

                while (true) {
                    val y = ((surfaceView.height - 60) / 2f) +60f -2f
                    dumpInfo(surfaceView)
                    delay(1000)
                    val holder = surfaceView.holder ?: return@launch
                    val canvas: Canvas? = holder.lockCanvas()
                    println(canvas)
                    if (canvas != null) {
                        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
                        canvas.drawColor(Color.GREEN)
                        val str = "hello world $index"
                        p.setColor(color.getColorForState(surfaceView.drawableState, 0))
                        canvas.drawCircle(10f ,10f ,5f  ,p)
                        canvas.drawText(str, 0, str.length, 30f, y, p)
                    }
                    index++
                    holder.unlockCanvasAndPost(canvas)
                }
            }
        }

        fun toggleInnerLayer() {
            frame.toggleLayer()
        }

        fun toggleHard() {
            frame.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        }

        fun View.toggleLayer() {
            if (this.layerType == View.LAYER_TYPE_SOFTWARE) {
                this.setLayerType(View.LAYER_TYPE_NONE, null)
            } else {
                this.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
            }

        }

    }


    fun View.toggleLayer() {
        if (this.layerType == View.LAYER_TYPE_SOFTWARE) {
            this.setLayerType(View.LAYER_TYPE_NONE, null)
        } else {
            this.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        }

    }
}